//
//  YJBLoginViewController.m
//  YunJiaBaoFirst
//
//  Created by 腾云小白龍 on 16/7/6.
//  Copyright © 2016年 腾云小白龍. All rights reserved.
//

#import "YJBLoginViewController.h"
#import "YJBLoginPhoneNumView.h"
#import "YJBRegisterViewController.h"
#import "YJBForgetPWViewController.h"
#import "YJBThirdLoginView.h"
#import "MainTabBarViewController.h"

@interface YJBLoginViewController()
@property (strong, nonatomic) UIScrollView * mainScrView;
@property (strong, nonatomic) UIButton * loginBtn;
@property (strong, nonatomic) YJBLoginPhoneNumView *phoneNumView;

@end

@implementation YJBLoginViewController

-(void)viewDidLoad{
    [super viewDidLoad];
    
    [self initSubViews];

    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

}

-(void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBar.hidden = YES;
    
}

#pragma mark-- 初始化Views
-(void)initSubViews{
    self.mainScrView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, -22, KMainScreenSize.width, KMainScreenSize.height+ 22)];
    self.mainScrView.contentSize = CGSizeMake(KMainScreenSize.width, KMainScreenSize.height);
    self.mainScrView.scrollEnabled = YES;
    self.mainScrView.showsVerticalScrollIndicator   = NO;
    self.mainScrView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:self.mainScrView];
    
    UIImageView *mainImgView = [[UIImageView alloc]initWithFrame:self.mainScrView.bounds];
    mainImgView.image = [UIImage imageNamed:@"LoginBackGround"];
    [self.mainScrView addSubview:mainImgView];
    
    CGFloat marginHeight = 70;
    if (KMainScreenSize.height == 480) {
        marginHeight = 50;
    }
    
    CGFloat imgViewW = KMainScreenSize.width * 120/375;
    CGFloat imgViewX = (KMainScreenSize.width - imgViewW)/2;
    CGFloat imgViewY = KMainScreenSize.height * 110/667;
    CGFloat imgViewH = imgViewW;
    UIImageView *logoImgView = [[UIImageView alloc]initWithFrame:CGRectMake(imgViewX, imgViewY,imgViewW,imgViewH)];
    logoImgView.image = [UIImage imageNamed:@"headImg"];
    logoImgView.layer.borderColor = KColorWithRGBA(238, 37, 51, 1).CGColor;
    logoImgView.layer.borderWidth = 4;
    logoImgView.layer.cornerRadius = imgViewW / 2;
    logoImgView.layer.masksToBounds = YES;
    [self.mainScrView addSubview:logoImgView];
    
    YJBLoginPhoneNumView * phoneNumView = [[YJBLoginPhoneNumView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(logoImgView.frame) + KMainScreenSize.height * marginHeight/667,KMainScreenSize.width,KMainScreenSize.height * 190/667)];
    self.phoneNumView = phoneNumView;
    phoneNumView.backgroundColor = KColorWithRGBA(72, 72, 72, 1);
    [phoneNumView YJBLoginPhoneNumView:phoneNumView withType:YES];
    [self.mainScrView addSubview:phoneNumView];
    

    [phoneNumView.loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];

    [phoneNumView.forgetPassWordBtn addTarget:self action:@selector(forgetPassWordBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [phoneNumView.createNewUserBtn addTarget:self action:@selector(registerBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    YJBThirdLoginView *thirdLoginView = [[YJBThirdLoginView alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(phoneNumView.frame) + 15, KMainScreenSize.width - 30,KMainScreenSize.height * 120/667)];
    [thirdLoginView YJBThirdLoginView:thirdLoginView];
    [self.mainScrView addSubview:thirdLoginView];
    
    
}

#pragma mark -- 登陆
-(void)loginBtnClick{
    
    MainTabBarViewController *vc = [[MainTabBarViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
    
    BOOL isPhoneNum = [HTUtils isMobileNumber:self.phoneNumView.phoneNumTextField.text];
    if (!self.phoneNumView.phoneNumTextField.text.length) {
        [SVProgressHUD showImage:nil status:@"请输入手机号码"];
        [self performSelector:@selector(SVProgressHUDdismiss) withObject:nil afterDelay:1.0f];
        return;
    }
    
    if (!isPhoneNum) {
        [SVProgressHUD showErrorWithStatus:@"您输入的手机号码不合法"];
        [self performSelector:@selector(SVProgressHUDdismiss) withObject:nil afterDelay:1.0f];
        return;
    }
    
    if (!self.phoneNumView.passwordTextField.text.length) {
        [SVProgressHUD showImage:nil status:@"请输入密码"];
        [self performSelector:@selector(SVProgressHUDdismiss) withObject:nil afterDelay:1.0f];
        return;

    }
    
}


#pragma mark -- 忘记密码
-(void)forgetPassWordBtnClick{
    YJBForgetPWViewController *VC = [[YJBForgetPWViewController alloc] init];
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController pushViewController:VC animated:YES];
}

#pragma mark -- 注册新用户
-(void)registerBtnClick{
    YJBRegisterViewController *VC = [[YJBRegisterViewController alloc] init];
    self.navigationController.navigationBar.hidden = NO;
    [self.navigationController pushViewController:VC animated:YES];
}


@end
